import {Component, OnInit} from '@angular/core';
import {GlobalService} from '../../service/GlobalService';
import {LsUtil} from '../../util/LsUtil';
import {LogUtil} from '../../util/LogUtil';
import {NzModalService} from 'ng-zorro-antd';
import {ForgetPswComponent} from '../login/forget-psw/forget-psw.component';

@Component({
  selector: 'app-main',
  templateUrl: './main.component.html',
  styleUrls: ['./main.component.less']
})
export class MainComponent implements OnInit {


  isCollapsed = false;
  userInfo;
  url = location.href;
  loginPass = 60 * 24; // mins

  constructor(public app: GlobalService, private ls: LsUtil, private modalService: NzModalService) {
  }

  ngOnInit() {
    this.userInfo = this.app.getUserInfo();
    console.log(this.userInfo);
    if (this.userInfo === false || new Date().getTime() - this.userInfo.loginTime > (this.loginPass * 60 * 1000)) {
      this.app.err('登录过期,请重新登陆');
      this.app.delUserInfo();
      this.app.router.navigate(['/login']);
      return;
    }


  }

  menuClick(i) {
    this.userInfo = this.app.getUserInfo();
    console.log(this.userInfo);
    if (this.userInfo === false || new Date().getTime() - this.userInfo.loginTime > (this.loginPass * 60 * 1000)) {
      this.app.err('登录过期,请重新登陆');
      this.app.delUserInfo();
      this.app.router.navigate(['/login']);
      return;
    }
    switch (i) {
      default:
        break;
      case 1:
        this.app.router.navigate(['main/admin']);
        break;
    }
  }

  logout() {
    this.app.delUserInfo();
    this.app.router.navigate(['']);
  }


  resetPsw() {
    LogUtil.o('forget');

    const modal = this.modalService.create({
      nzTitle: '修改密码',
      nzContent: ForgetPswComponent,
      nzComponentParams: {
        phone: this.userInfo.phone,

      },
      nzFooter: [{
        label: '确定',
        shape: 'primary',
        onClick: (c) => {
          c.setPsw();

        }
      },
        {
          label: '取消',
          onClick: () => {
            modal.destroy();
          }

        }]
    });

  }

}
